package com.awen.sqlSession;

import com.awen.io.Resources;
import com.awen.pojo.Configuration;

import java.beans.IntrospectionException;
import java.io.InputStream;
import java.lang.reflect.*;
import java.sql.SQLException;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementid, Object... params) {
        //将要去完成对simpleExecutor里的query方法的调用
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        List<Object> list = null;
        try {
            list = simpleExecutor.query(configuration, configuration.getMappedStatementMap().get(statementid), params);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return (List<E>) list;
    }

    @Override
    public <T> T selectOne(String statementid, Object... params) {
        List<Object> list = selectList(statementid, params);
        if(list.size() == 1){
            return (T) list.get(0);
        }else {
            throw new RuntimeException("查询结果为空或者返回结果过多");
        }
    }

    @Override
    public Integer updateOne(String statementid, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        Integer update = simpleExecutor.update(configuration, configuration.getMappedStatementMap().get(statementid), params);
        return update;
    }
    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //使用JDK动态代理来为Dao接口生成代理对象，并返回
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(),
                new Class[]{mapperClass}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        //底层都还是执行JDBC代码
                        //准备参数 1.statementid
                        String methodName = method.getName();
                        String className = method.getDeclaringClass().getName();
                        String statementId = className+"."+methodName;
                        //准备参数二: params:args
                        //调用方法的返回值类型
                        Type genericReturnType = method.getGenericReturnType();
                        //判断是否进行了 泛型类型参数化
                        if(genericReturnType instanceof ParameterizedType){
                            List<Object> objects = selectList(statementId, args);
                            return objects;
                        }else{
                            if(methodName.contains("insert") || methodName.contains("update") || methodName.contains("delete") ){
                                Integer integer = updateOne(statementId, args);
                                return integer;
                            }
                            Object o = selectOne(statementId, args);
                            return o;
                        }
                    }
                });
        return (T) proxyInstance;
    }
}
